package com.jdew.thkj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jdew.thkj.R;
import com.jdew.thkj.bean.JDListBean;
import com.jdew.thkj.bean.SuperSearchBean;
import com.jdew.thkj.onitemInterface.OnItemClickListener;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yangyang on 2017/9/18.
 */

public class JDLeiMuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    public List<JDListBean.DataBean> dataList;
    private LayoutInflater layoutInflater;
    private Context context;
    private View itemView;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private OnItemClickListener mItemClickListener;
    private String maType;
    private int height;

    @Override
    public void onClick(View v) {
        if (mItemClickListener != null) {
            mItemClickListener.onItemClick((Integer) v.getTag());
        }
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }

    public JDLeiMuAdapter(Context context, List<JDListBean.DataBean> dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(context);
        sp = context.getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        maType = sp.getString("SpareTwo", "");
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        height = dm.widthPixels/2-10;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setDataList(List<JDListBean.DataBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        itemView = layoutInflater.from(context).inflate(R.layout.jd_product_list_item2, parent, false);
        itemView.setOnClickListener(this);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myviewHolder = (MyViewHolder) holder;
        myviewHolder.BindView(holder, position);
        myviewHolder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.product)
        ImageView product;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.price)
        TextView price;
        @Bind(R.id.before_price)
        TextView beforePrice;
        @Bind(R.id.quan)
        TextView quan;
        @Bind(R.id.earn)
        TextView earn;
        @Bind(R.id.num)
        TextView num;
        @Bind(R.id.source)
        ImageView source;
        @Bind(R.id.earn_view)
        LinearLayout earn_view;

        MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) product.getLayoutParams();
            params.width=height;
            params.height=height;
            product.setLayoutParams(params);
        }

        public void BindView(RecyclerView.ViewHolder holder, int position) {
            DecimalFormat df = new DecimalFormat("0.00");
            DecimalFormat df1 = new DecimalFormat("0");
            String quan = dataList.get(position).getDiscount();
            ((MyViewHolder) holder).beforePrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            ((MyViewHolder) holder).beforePrice.setText(dataList.get(position).getWlPrice());
            ((MyViewHolder) holder).price.setText(df.format(Double.parseDouble(dataList.get(position).getWlPrice()) - Double.parseDouble(quan)) + "元");
            Double ppp = Double.parseDouble(dataList.get(position).getWlPrice()) - Double.parseDouble(quan);
            if (maType.equals("2")) {
                ((MyViewHolder) holder).earn.setText("  赚   ¥ " + df.format((Double.parseDouble(dataList.get(position).getWlPrice()) - Double.parseDouble(quan)) * Double.parseDouble(dataList.get(position).getWlCommissionShare()) / 200));
            } else {
                ((MyViewHolder) holder).earn.setText("  赚   ¥ " + df.format((Double.parseDouble(dataList.get(position).getWlPrice()) - Double.parseDouble(quan)) * Double.parseDouble(dataList.get(position).getWlCommissionShare()) / 100));
            }

             source.setImageResource(R.mipmap.jd_source);
             ((MyViewHolder) holder).name.setText(dataList.get(position).getSkuName());
//            ((MyViewHolder) holder).num.setText("月销:" + dataList.get(position).getSales());
             ((MyViewHolder) holder).num.setVisibility(View.INVISIBLE);
             ((MyViewHolder) holder).quan.setText("券   " + df1.format(Double.parseDouble(quan)) + "元");
            if (maType.equals("") || maType.equals("1")) {
                earn_view.setVisibility(View.GONE);
                SpannableString spannableString = new SpannableString(price.getText().toString());
                RelativeSizeSpan sizeSpan = new RelativeSizeSpan(1.2f);
                spannableString.setSpan(sizeSpan, 0, price.getText().toString().indexOf("."), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                price.setText(spannableString);
            } else {
                earn_view.setVisibility(View.VISIBLE);
                SpannableString spannableString = new SpannableString(price.getText().toString());
                RelativeSizeSpan sizeSpan = new RelativeSizeSpan(1.2f);
                spannableString.setSpan(sizeSpan, 0, price.getText().toString().indexOf("."), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                price.setText(spannableString);
            }
            Glide.with(context).load(dataList.get(position).getPicUrl()).into(product);
        }
    }

    public static class FootViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.loading)
        TextView mTvLoadText;
        @Bind(R.id.progressBar)
        ProgressBar progressBar;

        public FootViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, itemView);
        }
    }
}

